//
//  ZPStateCompleteViewController.m
//  ZPIOSEducation
//
//  Created by mini1 on 2016/12/12.
//  Copyright © 2016年 mini1. All rights reserved.
//

#import "ZPStateCompleteViewController.h"
#import "ZPStateCompleteTabCell.h"
#import "ZPStateReadyTabCell.h"
#import "MJRefresh.h"
#import "ZPMineTiaoKeViewController.h"
#import "ZPMineConfirmRefundVC.h"
#import "MapViewController.h"
#import "ZPSelectClassVC.h"
#import "ZPMineContinueVC.h"
#define completeClassTableViewSectionCount 1
#define completeClassTableViewRowCount 8
#define completeClassTableViewHeightForHeader 1
#define completeClassTableViewHeightForFooter 8

static NSString * const reuseIdentifier_ready = @"ZPStateReadyTabCell";

@interface ZPStateCompleteViewController () <UITableViewDataSource,UITableViewDelegate,ZPStateReadyTabCellDelegate>
{
    NSMutableArray *dataArray;
}

@property (weak, nonatomic) IBOutlet UIView *emptyView;
@property (weak, nonatomic) IBOutlet UITableView *completeClassTableView;
@property (assign,nonatomic) NSInteger pagenumber;
@property (assign,nonatomic) BOOL hasNextPage;
@end

@implementation ZPStateCompleteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    hud = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:hud];
    dataArray = [NSMutableArray array];
    
    _pagenumber = 1;
    __weak typeof(self) weakSelf = self;
    [_completeClassTableView addLegendFooterWithRefreshingBlock:^{
        if (weakSelf.hasNextPage) {
            weakSelf.pagenumber++;
            [weakSelf getMyClass];
        }else{
            if (weakSelf.completeClassTableView.footer.isRefreshing) {
                [weakSelf.completeClassTableView.footer endRefreshing];
            }
        }
    }];
    
    
    [self getMyClass];
    
}
#pragma mark - <viewWillAppear>
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.completeClassTableView.dataSource = self;
    self.completeClassTableView.delegate   = self;
}

-(void)getMyClass{
    [hud showAnimated:YES];
    [[ZPNetworking sharedZPNetworking] GET:APIUC@"class/getMyClass.json" parameters:@{@"id":[[[ZPAuthStore sharedZPAuthStore] userToDic] objectForKey:@"stuId"],@"pagesize":@"5",@"pagenumber":[NSString stringWithFormat:@"%li",(long)self.pagenumber]} progress:nil success:^(id  _Nonnull responseObject) {
        [hud hideAnimated:YES];
        if (_completeClassTableView.footer.isRefreshing) {
            [_completeClassTableView.footer endRefreshing];
        }
        if (CodeIs200(responseObject)) {
            
            NSArray * arr = [[responseObject objectForKey:@"result"] objectForKey:@"result"];
            for (NSDictionary *tmp in arr) {
                if ([toStr([tmp objectForKey:@"type"])isEqualToString:@"3"]) {
                    [dataArray addObject:[NSMutableDictionary changeType:tmp]];
                }
            }
            if (dataArray.count==0) {
                _emptyView.hidden=NO;
                _completeClassTableView.hidden=YES;
            }else{
                _emptyView.hidden=YES;
                _completeClassTableView.hidden=NO;
            }
            _hasNextPage = [[[responseObject objectForKey:@"result"] objectForKey:@"hasNextPage"] boolValue];
            _pagenumber = [[[responseObject objectForKey:@"result"] objectForKey:@"pageNumber"] integerValue];
            if (!_hasNextPage) {
                [_completeClassTableView.footer noticeNoMoreData];
            }else{
                [_completeClassTableView.footer resetNoMoreData];
            }
            [_completeClassTableView reloadData];
        }else{
            [ZPPromptView showMessage:toStr([responseObject objectForKey:@"codeMsg"])];
            
        }
    } failure:^(NSError * _Nonnull error) {
        if (_completeClassTableView.footer.isRefreshing) {
            [_completeClassTableView.footer endRefreshing];
        }
        [hud hideAnimated:YES];
    }];
}


#pragma mark - <UITableViewDatasource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  1 ;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [dataArray objectAtIndex:indexPath.row];
    ZPStateReadyTabCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_ready];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ZPStateReadyTabCell" owner:self options:nil] lastObject];
        cell.delegate=self;
    }
    cell.teachername.text = toStr([dic objectForKey:@"teacherName"]);
    cell.progress.text = [NSString stringWithFormat:@"已完成：%@",[dic objectForKey:@"progress"]];
cell.idxP = indexPath;
    switch ([[dic objectForKey:@"type"] integerValue]) {
        case 1:
            cell.type.text = @"学习中";
            cell.type.textColor=UIColorFromRGB(0x21a4eb);
            break;
        case 2:
            cell.type.text = @"待开课";
            cell.type.textColor=UIColorFromRGB(0xff5656);
            break;
        case 3:
            cell.type.text = @"已完成";
            cell.type.textColor=UIColorFromRGB(0x21a4eb);
            break;
        default:
            cell.type.text = @"";
            break;
    }
    cell.name.text = toStr([dic objectForKey:@"name"]);
    cell.dateString.text = toStr([dic objectForKey:@"dateString"]);
    cell.classNameL.text = toStr([dic objectForKey:@"className"]);
    cell.buttonType = [dic objectForKey:@"buttonType"];
    if ([[dic objectForKey:@"imgUrl"] isEqual:[NSNull null]]) {
        cell.mapL.hidden=YES;
        cell.mapBtn.hidden=YES;
        cell.mapI.hidden=YES;
    }else{
        cell.mapL.hidden=NO;
        cell.mapBtn.hidden=NO;cell.mapI.hidden=NO;    }
    return cell;
}


#pragma mark - <UITableViewDelegate>
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)tuiban:(NSIndexPath *)idxP{
    //    ZPMineTuifeiVC *vc = [[ZPMineTuifeiVC alloc] init];
    //    [self.navigationController pushViewController:vc animated:YES];
    
    
    
    NSDictionary *tmp = [dataArray objectAtIndex:idxP.row];
    [hud showAnimated:YES];
    [[ZPNetworking sharedZPNetworking] GET:APIUC@"quitclass/getquitde.json" parameters:@{@"stuId":[[[ZPAuthStore sharedZPAuthStore] userToDic] objectForKey:@"stuId"],@"classId":[tmp objectForKey:@"id"]} progress:nil success:^(id  _Nonnull responseObject) {
        [hud hideAnimated:YES];
        if (CodeIs200(responseObject)) {
            NSDictionary *result = [responseObject objectForKey:@"result"];
            ZPMineConfirmRefundVC *vc = [[ZPMineConfirmRefundVC alloc] init];
            vc.result = result;
            vc.classId = toStr([tmp objectForKey:@"id"]);
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [ZPPromptView showMessage:toStr([responseObject objectForKey:@"codeMsg"])];
            
        }
    } failure:^(NSError * _Nonnull error) {
        [ZPPromptView showMessage:error.localizedDescription];
        [hud hideAnimated:YES];
    }];
    
    
}

-(void)ditu:(NSIndexPath *)idxP{
    MapViewController *vc=[[MapViewController alloc]init];
    vc.imgUrl = toStr([[dataArray objectAtIndex:idxP.row] objectForKey:@"imgUrl"]);
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)zhuanban:(NSIndexPath *)idxP{
    [hud showAnimated:YES];
    NSDictionary *tmp =[dataArray objectAtIndex:idxP.row];
    [[ZPNetworking sharedZPNetworking] POST:APIUC@"adjust/getintoclasslist.json" parameters:@{@"stuId":[[[ZPAuthStore sharedZPAuthStore] userToDic] objectForKey:@"stuId"],@"classId":toStr([tmp objectForKey:@"id"])} progress:nil success:^(id  _Nonnull responseObject) {
        [hud hideAnimated:YES];
        if (CodeIs200(responseObject)) {
            ZPSelectClassVC *vc= [[ZPSelectClassVC alloc]init];
            vc.classId=toStr([tmp objectForKey:@"id"]);
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [ZPPromptView showMessage:toStr([responseObject objectForKey:@"codeMsg"])];
        }
    } failure:^(NSError * _Nonnull error) {
        [ZPPromptView showMessage:error.localizedDescription];
        [hud hideAnimated:YES];
    }];
}

-(void)tiaoke:(NSIndexPath *)idxP{
    [self  showHudithString:@"发送中..."];
    NSString  *classId = [[dataArray  objectAtIndex:idxP.row] objectForKey:@"id"];
    
    NSDictionary  *paramDic = [NSDictionary  dictionaryWithObjectsAndKeys:[[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"stuId"],@"stuId",[NSString stringWithFormat:@"%@",classId],@"classId", nil];
    [[ZPNetworking  sharedZPNetworking] POST:APIUC@"adjust/getownschedulelist.json" parameters:paramDic progress:^(NSProgress * _Nullable progress) {
        
    } success:^(id  _Nonnull responseObject) {
        NSLog(@"======%@",responseObject);
        [self  hiddenHud];
        
        if ([[responseObject  objectForKey:@"code"] integerValue] == 200) {
            if ([[responseObject  objectForKey:@"result"] isKindOfClass:[NSDictionary  class]]) {
                ZPMineTiaoKeViewController  *viewController = [[ZPMineTiaoKeViewController  alloc] initWithNibName:@"ZPMineTiaoKeViewController" bundle:nil];
                viewController.classId = [NSString stringWithFormat:@"%@",classId];
                viewController.dataDic = [NSDictionary  dictionaryWithDictionary:[responseObject  objectForKey:@"result"]];
                [self.navigationController  pushViewController:viewController animated:YES];
            }else {
                [ZPPromptView  showMessage:@"调课失败"];
            }
        }else {
            [ZPPromptView  showMessage:[responseObject  objectForKey:@"codeMsg"]];
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self  hiddenHud];
        
    }];
}

-(void)xubao:(NSIndexPath *)idxP{
    NSString  *classId = [[dataArray  objectAtIndex:idxP.row] objectForKey:@"id"];
    ZPMineContinueVC *viewController = [[ZPMineContinueVC alloc] initWithNibName:@"ZPMineContinueVC" bundle:nil];
    viewController.classId = [NSString stringWithFormat:@"%@",classId];
    [self.navigationController pushViewController:viewController animated:YES];
}

@end
